package com.yh.ldzl.config.websocket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket配置类
 * 配置WebSocket处理器和端点路径
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private QualityCheckWebSocketHandler webSocketHandler;
    
    @Autowired
    private WebSocketAuthInterceptor authInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册质检WebSocket端点，匹配前端请求路径 /ldzl-qc/websocket/qc
        registry.addHandler(webSocketHandler, "/ldzl-qc/websocket/qc", "/websocket/qc")
                .addInterceptors(authInterceptor)
                .setAllowedOrigins("*");  // 允许所有源，生产环境应配置具体的允许源
        
        // 注册通用WebSocket端点，匹配前端请求路径 /ldzl-qc/websocket
        registry.addHandler(webSocketHandler, "/ldzl-qc/websocket", "/websocket")
                .addInterceptors(authInterceptor)
                .setAllowedOrigins("*");
    }
}
